package com.itheima.tliasproject.controller;

import com.itheima.tliasproject.pojo.Dept;
import com.itheima.tliasproject.pojo.Result;
import com.itheima.tliasproject.service.Deptservice;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
    @Autowired
    private Deptservice deptservice;

    //查询部门信息
    @GetMapping("/depts")
    public Result list(){
        List<Dept> list = deptservice.List();
        log.info("查询全部数据成功！！！");
        return Result.Success(list);
    }
    //根据id删除部门
    @DeleteMapping("/depts")
    public Result delete(@RequestParam("id") Integer id){
        deptservice.delete(id);
        log.info("删除的部门id是：{}",id);
        return Result.Success();
    }

    //新增部门
    @PostMapping("/depts")
    public Result Insert(@RequestBody Dept dept){
        deptservice.Insert(dept);
        log.info("新增的部门是 {}",dept);
        return Result.Success();
    }

    //修改部门 分为两步 查询回显 然后修改
    //查询回显

    @GetMapping("/depts/{id}")
    public Result SeleById(@PathVariable Integer id){
        Dept dept = deptservice.GetById(id);
        log.info("查询的部门数据是 {}",id);
        return Result.Success(dept);
    }
    //修改部门
    @PutMapping("/depts")
    public Result Update(@RequestBody Dept dept){
        deptservice.Updata(dept);
        log.info("修改的部门信息是{}", dept);
        return Result.Success();
    }

}
